package com.play.base_activity.base_view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.gyf.barlibrary.ImmersionBar;
import com.play.airfight.R;


/**
 * code by SyShare
 * 2021/3/10
 */
public class TitleBar extends ConstraintLayout {
    private static int EXT_PADDING_TOP;
    private TextView mTitle;
    private ImageView mIcon;
    private ImageView mRightIv;
    private TextView tvRightName;
    private TextView tvLeftName;

    private View titleLine;


    public TitleBar(Context context) {
        super(context);
        init(null, 0, 0);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0, 0);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr, 0);
    }

    public void setStyleBold() {
        TextPaint tp = mTitle.getPaint();
        tp.setFakeBoldText(true);
    }

    private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        Context context = getContext();

        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.frame_include_title, this, true);

        tvLeftName = findViewById(R.id.tv_left_name);
        mTitle = findViewById(R.id.tv_titleName);
        mIcon = findViewById(R.id.iv_back);
        mRightIv = findViewById(R.id.iv_right);
        tvRightName = findViewById(R.id.tv_rightName);
        titleLine = findViewById(R.id.titleLine);

        if (attrs != null) {
            // Load attributes
            final TypedArray a = context.obtainStyledAttributes(
                    attrs, R.styleable.TitleBar, defStyleAttr, defStyleRes);

            String title = a.getString(R.styleable.TitleBar_aTitle);
            Drawable drawable = a.getDrawable(R.styleable.TitleBar_aIcon);
            Drawable rightDrawable = a.getDrawable(R.styleable.TitleBar_aRightIcon);
            String rightName = a.getString(R.styleable.TitleBar_aRightName);
            int titleColor = a.getColor(R.styleable.TitleBar_titleColor, Color.parseColor("#333333"));
            a.recycle();

            mTitle.setTextColor(titleColor);
            mTitle.setText(title);
            mIcon.setImageDrawable(drawable);
            if (rightDrawable != null) {
                mRightIv.setImageDrawable(rightDrawable);
                mRightIv.setVisibility(View.VISIBLE);
            }
            setRightName(rightName);
        } else {
            mIcon.setVisibility(GONE);
        }

        // Set Background
//        setBackgroundColor(getResources().getColor(R.color.white));

        // Init padding
        setPadding(getLeft(), ImmersionBar.getStatusBarHeight((Activity) getContext()), getRight(), getBottom());
    }

    public void setTitle(CharSequence titleRes) {
        if (TextUtils.isEmpty(titleRes)) {
            return;
        }
        mTitle.setText(titleRes);
    }

    public void setTitleEmpty() {
        mTitle.setText("");
    }

    @SuppressLint("ResourceType")
    public void setIcon(@DrawableRes int iconRes) {
        if (iconRes <= 0) {
            mIcon.setVisibility(GONE);
            return;
        }
        mIcon.setImageResource(iconRes);
        mIcon.setVisibility(VISIBLE);
    }

    @SuppressLint("ResourceType")
    public void setRightIcon(@DrawableRes int iconRes) {
        if (iconRes <= 0) {
            mRightIv.setVisibility(GONE);
            return;
        }
        mRightIv.setImageResource(iconRes);
        mRightIv.setVisibility(VISIBLE);
    }

    public void setRightIcon(Drawable iconDraw) {
        if (iconDraw == null) {
            mRightIv.setVisibility(GONE);
            return;
        }
        mRightIv.setImageDrawable(iconDraw);
        mRightIv.setVisibility(VISIBLE);
    }

    public void setLeftName(CharSequence leftName) {
        if (TextUtils.isEmpty(leftName)) {
            tvLeftName.setVisibility(View.GONE);
            return;
        }
        tvLeftName.setText(leftName);
        tvLeftName.setVisibility(View.VISIBLE);
    }

    public void setRightName(CharSequence rightName) {
        if (TextUtils.isEmpty(rightName)) {
            tvRightName.setVisibility(View.GONE);
            return;
        }
        tvRightName.setText(rightName);
        tvRightName.setVisibility(View.VISIBLE);
    }

    public void setRightName(String rightName) {
        if (TextUtils.isEmpty(rightName)) {
            tvRightName.setVisibility(View.GONE);
            return;
        }
        tvRightName.setText(rightName);
        tvRightName.setVisibility(View.VISIBLE);
    }

    public TextView getLeftTextView() {
        return tvLeftName;
    }

    public TextView getRightTextView() {
        return tvRightName;
    }

    public void setLeftNameClickListener(OnClickListener listener) {
        tvLeftName.setOnClickListener(listener);
    }

    public void setRightNameClickListener(OnClickListener listener) {
        tvRightName.setOnClickListener(listener);
    }

    public void setRightIconClickListener(OnClickListener listener) {
        mRightIv.setOnClickListener(listener);
    }

    public void setIconOnClickListener(OnClickListener listener) {
        mIcon.setOnClickListener(listener);
    }

    public View getTitleBarBottomLine() {
        return titleLine;
    }

    public ImageView getTitleLeftIconView() {
        return mIcon;
    }

    public TextView getTitleNameView() {
        return mTitle;
    }

    public TextView getTitleRightView() {
        return tvRightName;
    }


    public ImageView getTitleRightIconView() {
        return mRightIv;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        float d = getResources().getDisplayMetrics().density;
        int minH = (int) (d * 44 + ImmersionBar.getStatusBarHeight((Activity) getContext()));

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(minH, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setTitleTextColor(int parseColor) {
        mTitle.setTextColor(parseColor);
    }
}
